//
//  MapViewDelegate.swift
//  amap
//
//  Created by James Karl on 2020/5/8.
//

import Foundation
import MAMapKit

class MapViewDelegate  : NSObject, MAMapViewDelegate {
    private let handler: MapMessageHandler
    
    init(_ handler: MapMessageHandler) {
        self.handler = handler
    }
    
    func mapViewDidFinishLoadingMap(_ mapView: MAMapView!) {
        print("xxxxx map loaded")
        handler.sendJsonMessageToFlutter(message: ReplyToFlutter.success(id: MapMethods.onMapLoaded))
    }

    func mapView(_ mapView: MAMapView!, didSingleTappedAt coordinate: CLLocationCoordinate2D) {
        handler.sendJsonMessageToFlutter(message: ReplyToFlutter.success(id: MapMethods.onMapClicked, data: coordinate.toMap()))
    }
    
    func mapView(_ mapView: MAMapView!, didAnnotationViewTapped view: MAAnnotationView!) {
        handler.sendJsonMessageToFlutter(message: ReplyToFlutter.success(id: MapMethods.onMarkerClicked, data: view.toMap()))
    }
    
    func mapViewRequireLocationAuth(_ locationManager: CLLocationManager!) {
        locationManager.requestAlwaysAuthorization()
    }
    
    func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
        if let mapOverlay = overlay as? MapOverlay {
            return mapOverlay.createRenderer()
        } else {
            return nil
        }
    }
    
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        if let a = annotation as? MapAnnotation {
            return a.createAnnotationView(mapView: mapView, registrar: handler.registrar)
        }
        return nil
    }
    
}
